

let data = []

export const getList = (params = {}) => {
    return window.electron.getJSON("data.json").then(res => {
        data = res.data || []
        res.data = res.data.filter(item => {
            return !Object.keys(params).find((item1 =>
                !item[item1].includes(params[item1])
            ))
        })
        // for (let i = 0; i < 200; i++) {
        //     res.data.push({
        //         "name": "参赛选手" + i,
        //         "sex": "男",
        //         "id": "00" + i,
        //         "type": "半马",
        //         "score": 40,
        //         "speed": 7,
        //         "imageUrl": ""
        //     })
        // }
        return res
    }, (e) => {
        console.log(e)
        data = []
        return { data }
    })
}


export const save = (row) => {
    const index = data.findIndex(item => item.id == row.id)
    if (index !== -1) return Promise.reject({ success: false, msg: '参赛号已存在' })
    data.push(row)
    return window.electron.setJSON("data.json", data)
}



export const update = (row) => {
    const index = data.findIndex(item => item.id == row.id)
    if (index === -1) return Promise.reject({ success: false, msg: '编辑失败' })
    data[index] = row
    return window.electron.setJSON('data.json', data)
}


export const remove = (id) => {
    const index = data.findIndex(item => item.id == id)
    if (index === -1) return Promise.reject({ success: false, msg: '删除失败' })
    data.splice(index, 1)
    return window.electron.setJSON('data.json', data)
}


export const importData = (xlsxData) => {
    const newData = JSON.parse(JSON.stringify(data))
    xlsxData.forEach(item => {
        if (!item.id) return
        console.log(item, item.id)
        const resIndex = newData.findIndex(item1 => item1.id == item.id)
        console.log('resIndex', resIndex)
        if (resIndex !== -1) newData[resIndex] = item
        else newData.push(item)
    })
    return window.electron.setJSON("data.json", newData)
}
